﻿log("Running Test Task  ...  test.js")

loadScriptAsync("./utilities.js", main)


function main()
{
    log("Task : Test Task!")
    var href = location.href;

    if (has("s.taobao.com/search?"))
        onSearchResult();
    function has(pattern)
    {
        return href.search(pattern) != -1;
    }
}

function onSearchResult()
{
    log("Current Task : onSearchResult");

    doWithJQuery(function()
    {
        var ja = $(".next > a:nth-child(1)");
        var p = ja.offset();
        p = {
            top: p.top,
            left: p.left,
            right: p.left + ja.width(),
            bottom: p.top + ja.height()
        }

        var x = {
            x: p.left,
            y: p.top,
            height: p.bottom - p.top,
            width: p.right - p.left,
            centerX: (p.left + p.right) / 2,
            centerY: (p.top + p.bottom) / 2
        };
        x
        msg.send("searchResultShown", x);

        /*
        var a = document.querySelector(".next > a:nth-child(1)");
        var p = a.getBoundingClientRect();
        var sx = document.body.scrollLeft;
        var sy = document.body.scrollTop;
        var x = {
            sx: sx,
            sy: sy,
            x: p.left + sx,
            y: p.top + sy,
            height: p.bottom - p.top,
            width: p.right - p.left,
            centerX: (p.left + p.right) / 2 + sx,
            centerY: (p.top + p.bottom) / 2 + sy
        };
        msg.send("searchResultShown", x);
        */
    })

}

/*
 * Hareware.current : 
 * mouseX
 * mouseY
 * mouseClientX
 * mouseClientY
 * scrollX
 * scrollY
 * mouseOver
 * windowHeight
 * windowWidth
 * */
var current = {}
document.addEventListener("mouseover", function (e)
{
    current = {
        mouseClientX: e.clientX,
        mouseClientY: e.clientY,
        scrollX: window.scrollX,
        scrollY: window.scrollY,
        mouseOver: true,
        windowWidth: window.innerWidth || document.documentElement.clientWidth,
        windowHeight: window.innerHeight || document.documentElement.clientHeight,
        bodyWidth: document.body.offsetWidth,
        bodyHeight: document.body.offsetHeight
    }
});

document.body.offsetHeight

document.addEventListener("mouseout", function (e)
{
    current = {
        mouseClientX: e.clientX,
        mouseClientY: e.clientY,
        scrollX: window.scrollX,
        scrollY: window.scrollY,
        mouseOver: false,
        windowWidth: window.innerWidth || document.documentElement.clientWidth,
        windowHeight: window.innerHeight || document.documentElement.clientHeight,
        bodyWidth: document.body.offsetWidth,
        bodyHeight: document.body.offsetHeight
    }
});

document.addEventListener("mousemove", function (e)
{
    current = {
        mouseClientX: e.clientX,
        mouseClientY: e.clientY,
        scrollX: window.scrollX,
        scrollY: window.scrollY,
        mouseOver: true,
        windowWidth: window.innerWidth || document.documentElement.clientWidth,
        windowHeight: window.innerHeight || document.documentElement.clientHeight,
        bodyWidth: document.body.offsetWidth,
        bodyHeight: document.body.offsetHeight
    }
});

document.addEventListener("scroll", function (e)
{
    current = {
        mouseClientX: current.mouseClientX,
        mouseClientY: current.mouseClientY,
        scrollX: window.scrollX,
        scrollY: window.scrollY,
        mouseOver: true,
        windowWidth: window.innerWidth || document.documentElement.clientWidth,
        windowHeight: window.innerHeight || document.documentElement.clientHeight,
        bodyWidth: document.body.offsetWidth,
        bodyHeight: document.body.offsetHeight
    }
});


tasks.hareware_getCurrent = function (arg)
{
    msg.send("hareware_current", current)
}